package com.jingxing.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.jingxing.config.ApiResult;
import com.jingxing.config.UserLoginToken;
import com.jingxing.pojo.ReceivingRecord;
import com.jingxing.pojo.RewardReceive;
import com.jingxing.service.ReceivingRecordService;
import com.jingxing.service.RewardReceiveService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@Slf4j
@RestController
@RequestMapping("/rewardReceive")
@Api(value = "API", tags = {"领奖相关"})
@UserLoginToken
public class RewardReceiveController {
    @Autowired
    private RewardReceiveService rewardReceiveService;

    @Autowired
    private ReceivingRecordService receivingRecordService;

    @PostMapping("/consume")
    @ApiOperation("使用领奖次数")
    @Transactional
    public ApiResult consume(String playerId, String prizeId) {
        QueryWrapper<RewardReceive> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("player_id", playerId);
        queryWrapper.eq("prize_id", prizeId);
        RewardReceive one = rewardReceiveService.getOne(queryWrapper);
        if (one == null) {
            return ApiResult.no(one, "未找到领取信息，请于工作人员联系");
        }
        if (one.getReceiveSfrequency() == 0) {
            return ApiResult.no(null, "领取次数为0");
        }
        one.setReceiveSfrequency(one.getReceiveSfrequency() - 1);
        boolean updateById = rewardReceiveService.updateById(one);
        if (updateById) {
            ReceivingRecord receivingRecord = new ReceivingRecord();
            receivingRecord.setPlayerId(playerId);
            receivingRecord.setPrizeId(prizeId);
            boolean save = receivingRecordService.save(receivingRecord);
            return ApiResult.result(save);
        }
        return ApiResult.result(updateById);
    }

    @PostMapping("/getRewardReceives")
    @ApiOperation("获取奖品领取信息")
    public ApiResult getRewardReceives(String playerName, String prizeName) {
        List<RewardReceive> rewardReceives = rewardReceiveService.getRewardReceives(playerName, prizeName);
        return ApiResult.ok(rewardReceives);
    }

    @PostMapping("/updateRewardReceive")
    @ApiOperation("修改领奖次数")
    public ApiResult updateRewardReceive(String id, Integer type, String updateBy) {
        if (StringUtils.isBlank(id)) {
            return ApiResult.parameterError("ID不能为空");
        }
        if (type == null) {
            return ApiResult.parameterError("加减类型不能为空");
        }
        if (StringUtils.isBlank(updateBy)) {
            return ApiResult.parameterError("修改人ID不能为空");
        }
        RewardReceive rewardReceive = rewardReceiveService.getById(id);
        rewardReceive.setUpdateBy(updateBy);
        if (type == 0) {
            rewardReceive.setReceiveSfrequency(rewardReceive.getReceiveSfrequency() + 1);
        }
        else {
            if (rewardReceive.getReceiveSfrequency() == 0) {
                return ApiResult.no(null, "领取次数为0");
            }
            rewardReceive.setReceiveSfrequency(rewardReceive.getReceiveSfrequency() - 1);
        }
        boolean update = rewardReceiveService.updateById(rewardReceive);
        return ApiResult.result(update);
    }


}
